package com.example.demo.service.impl;

import com.example.demo.entity.dto.ApiTestCaseResultDto;
import com.example.demo.mapper.ApiTestCaseGroupMapper;
import com.example.demo.mapper.ApiTestCaseGroupResultMapper;
import com.example.demo.mapper.ApiTestCaseMapper;
import com.example.demo.mapper.ApiTestCaseMergeMapper;
import com.example.demo.entity.ApiTestCase;
import com.example.demo.entity.ApiTestCaseGroup;
import com.example.demo.entity.ApiTestCaseGroupResult;
import com.example.demo.entity.ApiTestCaseMerge;
import com.example.demo.entity.response.testCaseGroup.GroupResp;
import com.example.demo.service.ApiTestCaseGroupService;
import com.example.demo.service.ApiTestCaseStepService;
import com.example.demo.utils.DateToStamp;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.List;

@Service
public class ApiTestCaseGroupServiceImpl implements ApiTestCaseGroupService {

    @Resource
    ApiTestCaseGroupMapper apiTestCaseGroupMapper;

    @Resource
    ApiTestCaseMergeMapper apiTestCaseMergeMapper;

    @Resource
    ApiTestCaseMapper apiTestCaseMapper;

    @Resource
    ApiTestCaseStepService apiTestCaseStepService;
    @Resource
    ApiTestCaseGroupResultMapper apiTestCaseGroupResultMapper;

    ApiTestCaseGroupResult apiTestCaseGroupResult=new ApiTestCaseGroupResult();
    @Override
    public int insertApiTestCaseGroup(ApiTestCaseGroup apiTestCaseGroup){
        apiTestCaseGroup.setCreateTime(DateToStamp.getTimeStamp());
        apiTestCaseGroup.setUpdateTime(DateToStamp.getTimeStamp());
        return apiTestCaseGroupMapper.insertApiTestCaseGroup(apiTestCaseGroup);
    }

    @Override
    public void runTestGroup(int testCaseGroupId,int envId){

        int count=0,pass = 0,failed =0,skip=0;
        List<ApiTestCaseMerge> apiTestCaseMergeList=  apiTestCaseMergeMapper.findByGroupId(testCaseGroupId);
        count=apiTestCaseMergeList.size();
        apiTestCaseGroupResult.setCountResult(count);
        for (ApiTestCaseMerge apiTestCaseMerge: apiTestCaseMergeList){
            ApiTestCase apiTestCase=apiTestCaseMapper.findById(apiTestCaseMerge.getApiTestCaseId());
            if (apiTestCase.getIsSkip()){
                skip+=1;
            }else {
                ApiTestCaseResultDto apiTestCaseResultDto = apiTestCaseStepService.runStep(apiTestCaseMerge.getApiTestCaseId(), envId);
                if (apiTestCaseResultDto.getApiTestCaseResult().getFailedResults() > 0) {
                    failed += 1;
                } else {
                    pass += 1;
                }
            }
        }
        apiTestCaseGroupResult.setTestCaseGroupId(testCaseGroupId);
        apiTestCaseGroupResult.setFailedResult(failed);
        apiTestCaseGroupResult.setPassResult(pass);
        apiTestCaseGroupResult.setSkipResult(skip);
        apiTestCaseGroupResult.setCreateTime(DateToStamp.getTimeStamp());
        apiTestCaseGroupResult.setUpdateTime(DateToStamp.getTimeStamp());
        apiTestCaseGroupResultMapper.insertResult(apiTestCaseGroupResult);
    }

    @Override
    public void deleteCase(int id) {
        apiTestCaseMergeMapper.deleteById(id);
    }

    @Override
    public List<ApiTestCaseGroup> apiTestCaseGroupList(int projectId, String groupName) {
        return apiTestCaseGroupMapper.getGroupByProjectIdAndName(projectId,groupName);
    }

    @Override
    public List<GroupResp> caseListByGroupId(int groupId) {
        List<GroupResp> respList =new ArrayList<>();
        List<ApiTestCaseMerge> apiTestCaseMergeList =apiTestCaseMergeMapper.findByGroupId(groupId);
        if (apiTestCaseMergeList.size()>0){
            for (ApiTestCaseMerge apiTestCaseMerge :apiTestCaseMergeList){
                GroupResp groupResp = new GroupResp();
                groupResp.setId(apiTestCaseMerge.getId());
                groupResp.setGroupId(apiTestCaseMerge.getTestCaseGroupId());
                ApiTestCase apiTestCase =apiTestCaseMapper.findById(apiTestCaseMerge.getApiTestCaseId());
                groupResp.setTestCaseId(apiTestCase.getId());
                groupResp.setRemark(apiTestCase.getRemark());
                groupResp.setTestCaseName(apiTestCase.getName());
                respList.add(groupResp);
            }
        }
        return respList;
    }
}
